
package Server;
import java.net.*;
import java.io.*;
import java.util.Observable;
import java.util.Observer;

class ClientConnection extends Thread implements Observer{

   private Socket client = null;
   private ObjectOutputStream oos = null;
   private ObjectInputStream ois = null;
   private String inLine = null;
   private String outLine = "What's up";
   private DatabaseInterface daoInterface = new DatabaseInterface();
   public ClientConnection() {}
   public ClientConnection(Socket clientSocket) throws IOException {
        client = clientSocket;
        client.setSoTimeout(10000);
        daoInterface.addObserver( this );
        try {
            oos = new ObjectOutputStream(client.getOutputStream());
        } catch(Exception ex) {
            try {
                client.close();
            }catch (Exception e) {
                System.out.println(e.getMessage());
            }
            return;
        }
        try {
            ois = new ObjectInputStream(client.getInputStream());
        } catch(Exception ex) {
            try {
                client.close();
            }catch (Exception e) {
                System.out.println(e.getMessage());
            }
            return;
        }
        this.run();
    }
    public void run() {
       try {
           oos.writeObject("Good to go");
           while ((inLine = (String) ois.readObject()) != null){
               //inLine = (String) ois.readObject();
               oos.writeObject(inLine + " AND SEND THIS BACK");
               oos.flush();
               System.out.println(inLine);
           }
       }catch(Exception e) {
           System.out.println(e.getMessage());
       }
       try {
           oos.close();
           ois.close();
           client.close();
       }catch(Exception e) {
           System.out.println(e.getMessage());
       }
    }

    public void update (Observable obj, Object arg)
    {
        if (arg instanceof String)
        {
            String resp;
            resp = (String) arg;
            System.out.println("\nReceived Response: "+ resp );
        }
    }
}